package com.dove.ohpm.ui;

import com.dove.ohpm.ui.custom.OperateType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

public class view {
    private JPanel mainPanel;
    private JTextField textField;
    private JButton searchButton;
    private JScrollPane scrollPane;
    private JComboBox<String> comboBox;
    private JButton nextButton;
    private JComboBox<String> comboBox1;
    private JScrollPane scrollPane1;
    private JTabbedPane tabbedPane;
    private JCheckBox cbCustom;
    private JLabel lbMessage;
    private final JPanel containerPanel;
    private final JPanel containerPanel1;
    private Object[] packages;

    public JButton getNextButton() {
        return nextButton;
    }

    public JTextField getTextField() {
        return textField;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public JComboBox<String> getComboBox1() {
        return comboBox1;
    }

    public JButton getSearchButton() {
        return searchButton;
    }

    public JCheckBox getCbCustom() {
        return cbCustom;
    }

    public JLabel getLbMessage() {
        return lbMessage;
    }

    public view() {
        containerPanel = new JPanel();
        containerPanel.setLayout(new BoxLayout(containerPanel, BoxLayout.Y_AXIS));
        scrollPane.setViewportView(containerPanel);
        containerPanel1 = new JPanel();
        containerPanel1.setLayout(new BoxLayout(containerPanel1, BoxLayout.Y_AXIS));
        scrollPane1.setViewportView(containerPanel1);
        tabbedPane.addChangeListener(e ->
        {
            comboBox.removeAllItems();
            comboBox1.removeAllItems();
            for (Object name : this.packages) {
                comboBox.addItem(name.toString());
                comboBox1.addItem(name.toString());
            }
        });
    }

    public void clearChild() {
        try {
            containerPanel.removeAll();
            containerPanel.revalidate();
            containerPanel.repaint();
        } catch (Exception ex) {
            Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
        }
    }

    public void addChild(String name, String authorName, String description, String latestVersion, String keywords, String currentVersion, String packageId) {
        SwingUtilities.invokeLater(() -> {
            try {
                final var width = textField.getSize().width - 120;
                final JPanel column = new JPanel();
                column.setLayout(new BoxLayout(column, BoxLayout.X_AXIS));
                column.setBorder(JBUI.Borders.empty(8));
                JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                final var lbTitle = new JLabel();
                lbTitle.setSize(width, -1);
                viewExt.SetText(lbTitle, name + " by " + authorName);
                panel.add(lbTitle);
                final var lbDescription = new JLabel();
                lbDescription.setBorder(JBUI.Borders.emptyTop(8));
                lbDescription.setSize(width, -1);
                viewExt.SetText(lbDescription, description);
                panel.add(lbDescription);
                if (!keywords.isEmpty()) {
                    final var lbKeywords = new JLabel();
                    lbKeywords.setBorder(JBUI.Borders.emptyTop(8));
                    lbKeywords.setSize(width, -1);
                    viewExt.SetText(lbKeywords, keywords);
                    panel.add(lbKeywords);
                }
                column.add(panel);
                JPanel plButton = viewExt.getButtonPanel(name, latestVersion, currentVersion, packageId);
                column.add(plButton);
                containerPanel.add(column);
                containerPanel.revalidate();
                containerPanel.repaint();
            } catch (Exception ex) {
                Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
            }
        });
    }

    public void addPackages(java.util.List<String> names) {
        try {
            this.packages = names.toArray();
            for (Object name : names.toArray())
                comboBox.addItem(name.toString());
            viewExt.key = this.packages[0].toString();
            comboBox.addItemListener(e -> viewExt.key = e.getItem().toString());
            comboBox1.addItemListener(e -> viewExt.key = e.getItem().toString());
        } catch (Exception ex) {
            Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
        }
    }

    public void dependencies(Map<String, String> map) {
        try {
            final var size = new Dimension(78, 34);
            final var width = textField.getSize().width - 120;
            containerPanel1.removeAll();
            map.forEach((name, value) -> {
                JPanel column = new JPanel();
                column.setLayout(new BoxLayout(column, BoxLayout.X_AXIS));
                column.setBorder(JBUI.Borders.empty(8));
                final var lbTitle = new JLabel();
                lbTitle.setSize(width, -1);
                viewExt.SetText(lbTitle, name + "\t[" + value + "]");
                column.add(lbTitle);
                final var btnDel = new JButton();
                btnDel.setSize(size);
                btnDel.setText("移除");
                btnDel.addActionListener(e -> {
                    btnDel.setEnabled(false);
                    viewExt.clickButton(OperateType.Delete, viewExt.key, name, "");
                });
                column.add(btnDel);
                containerPanel1.add(column);
            });
            containerPanel1.revalidate();
            containerPanel1.repaint();
        } catch (Exception ex) {
            Messages.showErrorDialog((Project) null, ex.getMessage(), "Error");
        }
    }
}